#include "ActivityItemModel.h"

ActivityItemModel::ActivityItemModel(QList<QVariant> headData, QObject *parent)
    : QAbstractItemModel(parent)
{
    this->m_roleNameMaping[ActivityItemModelRoleName] = "title";
    this->m_roleNameMaping[ActivityItemModelContextName] = "context";
    this->m_roleNameMaping[ActivityItemModelDescription] = "Description";
    this->m_roleNameMaping[ActivityItemModelFlaged] = "Flaged";
    rootItem = new ActivityItem(headData);
}

ActivityItemModel::~ActivityItemModel()
{
    delete rootItem;
}

QVariant ActivityItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    // FIXME: Implement me!
    if(orientation != Qt::Horizontal && role != Qt::DisplayRole) return QVariant();
    return  this->rootItem->data (section);
}

bool ActivityItemModel::setHeaderData(int section, Qt::Orientation orientation, QVariant &value, int role)
{
    if (value != headerData(section, orientation, role) && orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        // FIXME: Implement me!
        this->rootItem->setData (value,section);
        emit headerDataChanged(orientation, section, section);
        return true;
    }
    return false;
}

QModelIndex ActivityItemModel::index(int row, int column, const QModelIndex &parent) const
{
    // FIXME: Implement me!
    if(!hasIndex (row,column,parent)) return QModelIndex();
    ActivityItem *parentItem;
    if(!parent.isValid ())
    {
        parentItem = rootItem;
    }
    else
    {
        parentItem = static_cast<ActivityItem *>(parent.internalPointer ());
    }
    ActivityItem *childItem = parentItem->child (row);
    if(childItem) return createIndex (row,column,childItem);
    return QModelIndex();
}

QModelIndex ActivityItemModel::parent(const QModelIndex &index) const
{
    // FIXME: Implement me!
    if(!index.isValid ()) return QModelIndex();
    ActivityItem *item = static_cast<ActivityItem *>(index.internalPointer ());
    ActivityItem *ParentItem = item->parentItem ();
    if(ParentItem == rootItem) return QModelIndex();
    return createIndex (ParentItem->row (),0,ParentItem);
}

int ActivityItemModel::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid())
        return 0;

    // FIXME: Implement me!
    ActivityItem *parenItem ;
    if(parent.isValid ())
    {
        parenItem = static_cast<ActivityItem *>(parent.internalPointer ());
    }else
    {
        parenItem = rootItem;
    }
    return parenItem->childCount ();
}

int ActivityItemModel::columnCount(const QModelIndex &parent) const
{
    if (!parent.isValid())
        return 0;

    // FIXME: Implement me!
    return parent.column ();
}

QVariant ActivityItemModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    // FIXME: Implement me!
    ActivityItem *item = static_cast<ActivityItem*>(index.internalPointer());
    if(!item) return QVariant();
    return item->data (role-Qt::UserRole-1);
}

bool ActivityItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (data(index, role) != value) {
        // FIXME: Implement me!
        ActivityItem* item = static_cast<ActivityItem*>(index.internalPointer ());
        item->setData (value,role-Qt::UserRole-1);
        emit dataChanged(index, index, QVector<int>() << role);
        return true;
    }
    return false;
}

QVariant ActivityItemModel::setActivityTypeData(const ActivityItemType &value)
{

}


Qt::ItemFlags ActivityItemModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;

    return Qt::ItemIsEditable; // FIXME: Implement me!
}

bool ActivityItemModel::insertRows(int row, int count, const QModelIndex &parent)
{
    beginInsertRows(parent, row, row + count - 1);
    // FIXME: Implement me!

    endInsertRows();
}

bool ActivityItemModel::insertColumns(int column, int count, const QModelIndex &parent)
{
    beginInsertColumns(parent, column, column + count - 1);
    // FIXME: Implement me!
    endInsertColumns();
}

bool ActivityItemModel::removeRows(int row, int count, const QModelIndex &parent)
{
    beginRemoveRows(parent, row, row + count - 1);
    // FIXME: Implement me!
    endRemoveRows();
}

bool ActivityItemModel::removeColumns(int column, int count, const QModelIndex &parent)
{
    beginRemoveColumns(parent, column, column + count - 1);
    // FIXME: Implement me!
    endRemoveColumns();
}
